package com.lxyk.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 目标执行前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object loginUser = request.getSession().getAttribute("currentUser");
        if (loginUser==null){
            //未登录 返回登录界面
            request.setAttribute("msg","你没有权限进行此操作，请先登录！ ");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else {
            //放行
            return true;
        }

    }
}
